package controllayer;

import java.util.ArrayList;

import dblayer.DBConnection;
import dblayer.DBItem;
import dblayer.IFDBItem;
import modellayer.Item;
import modellayer.Supplier;
import modellayer.Type;

public class CtrItem {

	private IFDBItem dbObj;
	private CtrType ctrType;
	private CtrSupplier ctrSupplier;
	
	public CtrItem(){
		dbObj = new DBItem();
		ctrType = new CtrType();
		ctrSupplier = new CtrSupplier();
	}
	
	public ArrayList<Item> getAllItems(){
		return dbObj.getAllItems();
	}
	
	public Item searchByID(int id){
		return dbObj.searchByID(id);
	}
	
	public Item searchByName(String name){
		return dbObj.searchByName(name);
	}
	
	//
	
	public int updateItem(int id, String name, String description, double price, int amount, String status, int max, int min, Type type, Supplier supplier){
		Item item = new Item(id, name, description, price, amount, status, max, min, type, supplier);
		
		return dbObj.updateItem(item);
	}
	
	public void createItem(String name, String description, double price, int amount, String status, int max, int min, int type, int supplier){
		Item item = new Item(name, description, price, amount, status, max, min, ctrType.searchByID(type), ctrSupplier.searchByID(supplier));
		
		try{
			DBConnection.startTransaction();
			dbObj.insertItem(item);
			DBConnection.commitTransaction();
		}
		catch(Exception e){
			DBConnection.rollbackTransaction();
		}
	
	}
	
	public int deleteItem(int id){
		return dbObj.deleteItem(id);
	}
}
